package com.js.mytransitiondemo.adapter;

// Created by JS on 2021/7/6.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.js.mytransitiondemo.R;
import com.js.mytransitiondemo.model.Effect;

import java.util.List;

public class ShareElementListAdapter extends RecyclerView.Adapter<ShareElementListAdapter.ViewHolder> {

    private Context mContext;
    private List<Integer> mList;
    private OnItemClickListener mListener;

    public ShareElementListAdapter(Context context, List<Integer> list, OnItemClickListener listener) {
        this.mContext = context;
        this.mList = list;
        this.mListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_share_element_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ShareElementListAdapter.ViewHolder holder, int position) {
        Integer url = mList.get(position);
        Glide.with(mContext)
                .load(url)
                .apply(new RequestOptions().skipMemoryCache(true))
                .into(holder.img);
        holder.img.setOnClickListener(v -> {
            mListener.onItemClick(holder.img, url);
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int url);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView img;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.item_img);
        }
    }
}
